﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Windows.UI.Facade.MinisterService;
using StockholmsStad.Windows.UI.Facade.ParvisService;

namespace StockholmsStad.Windows.UI.Facade
{
    public class MinisterFacade : FacadeBase
    {
        /// <summary>
        /// Konstruktor. 
        /// </summary>
        /// <param name="user">Den inloggade användaren (<see cref="User"/>). </param>
        public MinisterFacade(User user) : base(user)
        {
            
        }

        public Minister GetMinister(int ministerId)
        {
            var client = new WinServiceClient();
            try
            {
                var minister = client.GetMinister(CurrentUser, ministerId);
                client.Close();
                return minister;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public int AddMinister(Minister minister)
        {
            var client = new WinServiceClient();
            try
            {
                var ministerId = client.AddMinister(CurrentUser, minister);
                client.Close();
                return ministerId;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public void UpdateMinister(Minister minister)
        {
            var client = new WinServiceClient();
            try
            {
                client.UpdateMinister(CurrentUser, minister);
                client.Close();

            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public List<MinisterTimeBlock> GetTimeBlocks(int ministerId)
        {
            var client = new WinServiceClient();
            List<MinisterTimeBlock> list;

            try
            {
                list = new List<MinisterTimeBlock>();
                var timeBlocks = client.GetTimeBlocksForMinister(CurrentUser, ministerId);
                list.AddRange(timeBlocks);
                client.Close();
                return list;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public bool HasMinisterServices(int ministerId, DateTime date)
        {
            var client = new WinServiceClient();

            try
            {
                var result = client.HasMinisterAnyServices(CurrentUser, ministerId, date);
                client.Close();
                return result;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }            
        }

        public List<Minister> GetMinisters()
        {
            var client = new MinisterServiceClient();

            try
            {
                return GetMinisters(string.Empty, null, null, true);
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public List<Minister> GetMinisters(string partOfName, DateTime? startDate, DateTime? endDate, bool isActive)
        {
            var client = new MinisterServiceClient();

            try
            {
                var ministers = new List<Minister>();
                var list = client.GetMinisters(CurrentUser, partOfName,  startDate, endDate, isActive);
                client.Close();
                ministers.AddRange(list);
                return ministers;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public ObservableCollection<Minister> GetMinistersForDate(DateTime date)
        {
            var client = new WinServiceClient();

            try
            {
                var list = client.GetMinistersForPeriod(CurrentUser, date);
                client.Close();
                return new ObservableCollection<Minister>(list);
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }
    }
}
